<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header">
        <el-button @click="$router.push({ name: 'menu-create' })"
          >添加菜单
        </el-button>
      </div>
      <el-table border :data="menuTable" style="width: 100%">
        <el-table-column type="index" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="菜单名称"></el-table-column>
        <el-table-column prop="level" label="菜单级数"></el-table-column>
        <el-table-column prop="icon" label="图标"></el-table-column>
        <el-table-column prop="orderNum" label="排序"> </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handleDelete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { getAllMenus, deleteMenu } from "@/services/menu";
export default Vue.extend({
  name: "MenuIndex",
  data() {
    return {
      menuTable: [],
    };
  },
  created() {
    this.getMenuTable();
  },
  methods: {
    async getMenuTable() {
      const {
        data: { code, data },
      } = await getAllMenus();
      if (code === "000000") {
        this.menuTable = data;
      }
    },
    handleEdit(item: any) {
      this.$router.push({ name: "menu-create", query: { id: item.id } });
    },
    async handleDelete() {
      try {
        const result = await this.$confirm("确定删除吗？", "删除提示", {});
        if (result === "confirm") {
          const {
            data: { code },
          } = await deleteMenu();
          if (code === "000000") {
            this.$message.success("删除成功！");
          }
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
});
</script>

<style lang="scss" scoped></style>
